package com.tl.commonlibrary.network;

import android.text.SpannableString;

import com.google.gson.Gson;
import com.tl.commonlibrary.AppConfig;
import com.tl.commonlibrary.tool.Logger;
import com.tl.commonlibrary.ui.beans.User;
import com.tl.commonlibrary.ui.user.Account;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author JongSung
 * @Description
 * @date 2017/11/23 0023
 */

public class BaseNet {
    public static final String tag = "Http";
    public static final int PAGE_SIZE = 10;
    public static final String KEY_PAGE_NUM = "pageNum";


    public static RequestBody getBody() {
        return getBody(null);
    }

    public static RequestBody getBody(HashMap<String, Object> map) {
        if (map == null) {
            map = new HashMap<>();
        }
        map.put("uid", Account.getUid());
        map.put("token", Account.getToken());
        if (AppConfig.DEBUG()) {
            Logger.i(tag, "post param =======>");
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                sb.append(entry.getKey() + "=" + entry.getValue() + "\n");
            }
            Logger.i(tag, sb.toString());
            Logger.i(tag, "post param <=======");
        }
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), getDataParam(map));
    }

    public static HashMap<String, Object> getQuery() {
        return getQuery(null);
    }

    public static HashMap<String, Object> getQuery(HashMap<String, Object> map) {
        if (map == null) {
            map = new HashMap<>();
        }
        map.put("uid", Account.getUid());
        map.put("token", Account.getToken());
        if (AppConfig.DEBUG()) {
            Logger.i(tag, "get param =======>");
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                sb.append("&" + entry.getKey() + "=" + entry.getValue());
            }
            Logger.i(tag, sb.toString());
            Logger.i(tag, "get param <=======");
        }
        return map;

    }

    public static String getDataParam() {
        return getDataParam(null);
    }

    private static String getDataParam(HashMap<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Gson gson = new Gson();
        return gson.toJson(map);
    }
}
